home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / base / models.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-28  |  11.5 KB  |  483 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from base.g import *
  5. from base import utils
  6. import os.path as os
  7. import re
  8. import glob
  9.  
  10. try:
  11.     import datetime
  12.     datetime_avail = True
  13. except ImportError:
  14.     datetime_avail = False
  15.     datetime = None
  16.  
  17. pat_prod_num = re.compile('(\\d+)', re.I)
  18. TYPE_UNKNOWN = 0
  19. TYPE_STRING = 1
  20. TYPE_STR = 1
  21. TYPE_LIST = 2
  22. TYPE_BOOL = 3
  23. TYPE_INT = 4
  24. TYPE_HEX = 5
  25. TYPE_BITFIELD = 6
  26. TYPE_URI = TYPE_STR
  27. TYPE_DATE = 8
  28. TECH_CLASSES = [
  29.     'Undefined',
  30.     'Unsupported',
  31.     'Postscript',
  32.     'DJGenericVIP',
  33.     'LJMono',
  34.     'LJColor',
  35.     'LJFastRaster',
  36.     'LJJetReady',
  37.     'DJ350',
  38.     'DJ540',
  39.     'DJ600',
  40.     'DJ6xx',
  41.     'DJ6xxPhoto',
  42.     'DJ630',
  43.     'DJ8xx',
  44.     'DJ8x5',
  45.     'DJ850',
  46.     'DJ890',
  47.     'DJ9xx',
  48.     'DJ9xxVIP',
  49.     'DJ3600',
  50.     'DJ3320',
  51.     'DJ4100',
  52.     'AP2xxx',
  53.     'AP21xx',
  54.     'AP2560',
  55.     'PSP100',
  56.     'PSP470',
  57.     'LJZjsMono',
  58.     'LJZjsColor',
  59.     'LJm1005',
  60.     'QuickConnect',
  61.     'DJ55xx',
  62.     'OJProKx50',
  63.     'LJP1XXX',
  64.     'DJD2600']
  65. TECH_CLASSES.sort()
  66. TECH_CLASS_PDLS = {
  67.     'Postscript': 'ps',
  68.     'DJGenericVIP': 'pcl3',
  69.     'LJMono': 'pcl3',
  70.     'LJColor': 'pcl3',
  71.     'LJFastRaster': 'pclxl',
  72.     'LJJetReady': 'pclxl',
  73.     'DJ350': 'pcl3',
  74.     'DJ540': 'pcl3',
  75.     'DJ600': 'pcl3',
  76.     'DJ6xx': 'pcl3',
  77.     'DJ6xxPhoto': 'pcl3',
  78.     'DJ630': 'pcl3',
  79.     'DJ8xx': 'pcl3',
  80.     'DJ8x5': 'pcl3',
  81.     'DJ850': 'pcl3',
  82.     'DJ890': 'pcl3',
  83.     'DJ9xx': 'pcl3',
  84.     'DJ9xxVIP': 'pcl3',
  85.     'DJ3600': 'lidil',
  86.     'DJ3320': 'lidil',
  87.     'DJ4100': 'lidil',
  88.     'AP2xxx': 'pcl3',
  89.     'AP21xx': 'pcl3',
  90.     'AP2560': 'pcl3',
  91.     'PSP100': 'pcl3',
  92.     'PSP470': 'pcl3',
  93.     'LJZjsMono': 'zjs',
  94.     'LJZjsColor': 'zjs',
  95.     'LJm1005': 'zxs',
  96.     'QuickConnect': 'jpeg',
  97.     'DJ55xx': 'pcl3',
  98.     'OJProKx50': 'pcl3',
  99.     'LJP1XXX': 'zxs' }
  100. PDL_TYPE_PCL = 0
  101. PDL_TYPE_PS = 1
  102. PDL_TYPE_HOST = 2
  103. PDL_TYPES = {
  104.     'pcl3': PDL_TYPE_PCL,
  105.     'pcl5': PDL_TYPE_PCL,
  106.     'pcl6': PDL_TYPE_PCL,
  107.     'pcl5e': PDL_TYPE_PCL,
  108.     'pcl': PDL_TYPE_PCL,
  109.     'pclxl': PDL_TYPE_PCL,
  110.     'ps': PDL_TYPE_PS,
  111.     'lidil': PDL_TYPE_HOST,
  112.     'zjs': PDL_TYPE_HOST,
  113.     'zjstream': PDL_TYPE_HOST,
  114.     'zxs': PDL_TYPE_HOST,
  115.     'zxstream': PDL_TYPE_HOST,
  116.     'jpeg': PDL_TYPE_HOST,
  117.     'jpg': PDL_TYPE_HOST,
  118.     'jetready': PDL_TYPE_HOST,
  119.     'jr': PDL_TYPE_HOST }
  120. TECH_SUBCLASSES = [
  121.     'LargeFormatSuperB',
  122.     'LargeFormatA3',
  123.     'CoverMedia',
  124.     'FullBleed',
  125.     'Duplex',
  126.     'Normal',
  127.     'Apollo2000',
  128.     'Apollo2200',
  129.     'Apollo2500',
  130.     'NoPhotoMode',
  131.     'NoPhotoBestHiresModes',
  132.     'No1200dpiNoSensor',
  133.     'NoFullBleed',
  134.     '4x6FullBleed',
  135.     '300dpiOnly',
  136.     'GrayscaleOnly',
  137.     'NoAutoTray',
  138.     'NoEvenDuplex']
  139. TECH_SUBCLASSES.sort()
  140. MODEL_UI_REPLACEMENTS = {
  141.     'laserjet': 'LaserJet',
  142.     'psc': 'PSC',
  143.     'hp': 'HP',
  144.     'mfp': 'MFP' }
  145.  
  146. def normalizeModelUIName(model):
  147.     ml = model.lower().strip()
  148.     if 'apollo' in ml:
  149.         z = ml.replace('_', ' ')
  150.     elif ml.startswith('hp'):
  151.         z = ml[3:].replace('_', ' ')
  152.     else:
  153.         z = ml.replace('_', ' ')
  154.     y = []
  155.     for x in z.split():
  156.         if pat_prod_num.search(x):
  157.             y.append(x)
  158.             continue
  159.         y.append(MODEL_UI_REPLACEMENTS.get(x, x.capitalize()))
  160.     
  161.     if 'apollo' in ml:
  162.         return ' '.join(y)
  163.     return 'HP ' + ' '.join(y)
  164.  
  165.  
  166. def normalizeModelName(model):
  167.     return utils.xstrip(model.replace(' ', '_').replace('__', '_').replace('~', '').replace('/', '_'), '_')
  168.  
  169.  
  170. class ModelData:
  171.     
  172.     def __init__(self, root_path = None):
  173.         if root_path is None:
  174.             self.root_path = prop.models_dir
  175.         else:
  176.             self.root_path = root_path
  177.         self._ModelData__cache = { }
  178.         self.reset_includes()
  179.         self.sec = re.compile('^\\[(.*)\\]')
  180.         self.inc = re.compile('^\\%include (.*)', re.I)
  181.         self.inc_line = re.compile('^\\%(.*)\\%')
  182.         self.eq = re.compile('^([^=]+)=(.*)')
  183.         self.date = re.compile('^(\\d{1,2})/(\\d{1,2})/(\\d{4,4})')
  184.         files = [
  185.             (os.path.join(self.root_path, 'models.dat'), os.path.join(self.root_path, 'unreleased', 'unreleased.dat')),
  186.             (os.path.join(os.getcwd(), 'data', 'models', 'models.dat'), os.path.join(os.getcwd(), 'data', 'models', 'unreleased', 'unreleased.dat'))]
  187.         for None in files:
  188.             (self.released_dat, self.unreleased_dat) = None
  189.             if os.path.exists(self.released_dat):
  190.                 break
  191.                 continue
  192.         else:
  193.             (self.released_dat, self.unreleased_dat) = (None, None)
  194.         self.FIELD_TYPES = {
  195.             'align-type': TYPE_INT,
  196.             'clean-type': TYPE_INT,
  197.             'color-cal-type': TYPE_INT,
  198.             'copy-type': TYPE_INT,
  199.             'embedded-server-type': TYPE_INT,
  200.             'fax-type': TYPE_INT,
  201.             'fw-download': TYPE_BOOL,
  202.             'icon': TYPE_STR,
  203.             'io-mfp-mode': TYPE_INT,
  204.             'io-mode': TYPE_INT,
  205.             'io-support': TYPE_BITFIELD,
  206.             'job-storage': TYPE_INT,
  207.             'monitor-type': TYPE_INT,
  208.             'linefeed-cal-type': TYPE_INT,
  209.             'panel-check-type': TYPE_INT,
  210.             'pcard-type': TYPE_INT,
  211.             'plugin': TYPE_INT,
  212.             'plugin-reason': TYPE_BITFIELD,
  213.             'power-settings': TYPE_INT,
  214.             'pq-diag-type': TYPE_INT,
  215.             'r-type': TYPE_INT,
  216.             'scan-style': TYPE_INT,
  217.             'scan-type': TYPE_INT,
  218.             'status-battery-check': TYPE_INT,
  219.             'status-dynamic-counters': TYPE_INT,
  220.             'status-type': TYPE_INT,
  221.             'support-subtype': TYPE_HEX,
  222.             'support-released': TYPE_BOOL,
  223.             'support-type': TYPE_INT,
  224.             'support-ver': TYPE_STR,
  225.             'tech-class': TYPE_LIST,
  226.             'tech-subclass': TYPE_LIST,
  227.             'tech-type': TYPE_INT,
  228.             'usb-pid': TYPE_HEX,
  229.             'usb-vid': TYPE_HEX,
  230.             'wifi-config': TYPE_INT }
  231.         self.FIELD_TYPES_DYN = {
  232.             'dev-file': TYPE_STR,
  233.             'fax-uri': TYPE_STR,
  234.             'scan-uri': TYPE_STR,
  235.             'is-hp': TYPE_BOOL,
  236.             'host': TYPE_STR,
  237.             'status-desc': TYPE_STR,
  238.             'cups-printers': TYPE_STR,
  239.             'serial': TYPE_STR,
  240.             'error-state': TYPE_INT,
  241.             'device-state': TYPE_INT,
  242.             'panel': TYPE_INT,
  243.             'device-uri': TYPE_STR,
  244.             'panel-line1': TYPE_STR,
  245.             'panel-line2': TYPE_STR,
  246.             'back-end': TYPE_STR,
  247.             'port': TYPE_INT,
  248.             'deviceid': TYPE_STR,
  249.             'cups-uri': TYPE_STR,
  250.             'status-code': TYPE_INT,
  251.             'rs': TYPE_STR,
  252.             'rr': TYPE_STR,
  253.             'rg': TYPE_STR,
  254.             'r': TYPE_INT,
  255.             'duplexer': TYPE_INT,
  256.             'supply-door': TYPE_INT,
  257.             'revision': TYPE_INT,
  258.             'media-path': TYPE_INT,
  259.             'top-door': TYPE_BOOL,
  260.             'photo-tray': TYPE_BOOL }
  261.         self.RE_FIELD_TYPES = {
  262.             re.compile('^r(\\d+)-agent(\\d+)-kind', re.IGNORECASE): TYPE_INT,
  263.             re.compile('^r(\\d+)-agent(\\d+)-type', re.IGNORECASE): TYPE_INT,
  264.             re.compile('^r(\\d+)-agent(\\d+)-sku', re.IGNORECASE): TYPE_STR,
  265.             re.compile('^agent(\\d+)-desc', re.IGNORECASE): TYPE_STR,
  266.             re.compile('^agent(\\d+)-virgin', re.IGNORECASE): TYPE_BOOL,
  267.             re.compile('^agent(\\d+)-dvc', re.IGNORECASE): TYPE_INT,
  268.             re.compile('^agent(\\d+)-kind', re.IGNORECASE): TYPE_INT,
  269.             re.compile('^agent(\\d+)-type', re.IGNORECASE): TYPE_INT,
  270.             re.compile('^agent(\\d+)-id', re.IGNORECASE): TYPE_INT,
  271.             re.compile('^agent(\\d+)-hp-ink', re.IGNORECASE): TYPE_BOOL,
  272.             re.compile('^agent(\\d+)-health-desc', re.IGNORECASE): TYPE_STR,
  273.             re.compile('^agent(\\d+)-health$', re.IGNORECASE): TYPE_INT,
  274.             re.compile('^agent(\\d+)-known', re.IGNORECASE): TYPE_BOOL,
  275.             re.compile('^agent(\\d+)-level', re.IGNORECASE): TYPE_INT,
  276.             re.compile('^agent(\\d+)-ack', re.IGNORECASE): TYPE_BOOL,
  277.             re.compile('^agent(\\d+)-sku', re.IGNORECASE): TYPE_STR,
  278.             re.compile('^in-tray(\\d+)', re.IGNORECASE): TYPE_BOOL,
  279.             re.compile('^out-tray(\\d+)', re.IGNORECASE): TYPE_BOOL,
  280.             re.compile('^model(\\d+)', re.IGNORECASE): TYPE_STR }
  281.         self.TYPE_CACHE = { }
  282.  
  283.     
  284.     def read_all_files(self, unreleased = True):
  285.         if os.path.exists(self.released_dat):
  286.             self.read_section(self.released_dat)
  287.             if self.unreleased_dat is not None and os.path.exists(self.unreleased_dat):
  288.                 self.read_section(self.unreleased_dat)
  289.             
  290.         
  291.         return self._ModelData__cache
  292.  
  293.     
  294.     def read_section(self, filename, section = None, is_include = False):
  295.         found = False
  296.         in_section = False
  297.         if section is not None:
  298.             section = section.lower()
  299.             if is_include:
  300.                 log.debug('Searching for include [%s] in file %s' % (section, filename))
  301.             else:
  302.                 log.debug('Searching for section [%s] in file %s' % (section, filename))
  303.         
  304.         if is_include:
  305.             cache = self._ModelData__includes
  306.         else:
  307.             cache = self._ModelData__cache
  308.         
  309.         try:
  310.             fd = file(filename)
  311.         except IOError:
  312.             e = None
  313.             log.error('I/O Error: %s (%s)' % (filename, e.strerror))
  314.             return False
  315.  
  316.         while True:
  317.             line = fd.readline()
  318.             if not line:
  319.                 break
  320.             
  321.             if line[0] in ('#', ';'):
  322.                 continue
  323.             
  324.             if line[0] == '[':
  325.                 if in_section and section is not None:
  326.                     break
  327.                 
  328.                 match = self.sec.search(line)
  329.                 if match is not None:
  330.                     in_section = True
  331.                     read_section = match.group(1).lower()
  332.                     if section is not None:
  333.                         found = in_section = read_section == section
  334.                     
  335.                     if in_section:
  336.                         if section is not None:
  337.                             log.debug('Found section [%s] in file %s' % (read_section, filename))
  338.                         
  339.                         cache[read_section] = { }
  340.                     
  341.                 in_section
  342.                 continue
  343.             
  344.             if line[0] == '%':
  345.                 match = self.inc.match(line)
  346.                 if match is not None:
  347.                     inc_file = match.group(1)
  348.                     log.debug('Found include file directive: %%include %s' % inc_file)
  349.                     self._ModelData__include_files.append(os.path.join(os.path.dirname(filename), inc_file))
  350.                     continue
  351.                 
  352.                 if in_section:
  353.                     match = self.inc_line.match(line)
  354.                     if match is not None:
  355.                         inc_sect = match.group(1)
  356.                         log.debug('Found include directive %%%s%%' % inc_sect)
  357.                         
  358.                         try:
  359.                             self._ModelData__includes[inc_sect]
  360.                         except KeyError:
  361.                             for inc in self._ModelData__include_files:
  362.                                 if self.read_section(inc, inc_sect, True):
  363.                                     break
  364.                                     continue
  365.                             
  366.                         
  367.  
  368.                     None<EXCEPTION MATCH>KeyError
  369.                 
  370.             
  371.             if in_section:
  372.                 match = self.eq.search(line)
  373.                 if match is not None:
  374.                     key = match.group(1)
  375.                     value = match.group(2)
  376.                     value = self.convert_data(key, value)
  377.                     cache[read_section][key] = value
  378.                 
  379.             match is not None
  380.         fd.close()
  381.         return found
  382.  
  383.     
  384.     def reset_includes(self):
  385.         self._ModelData__include_files = []
  386.         self._ModelData__includes = { }
  387.  
  388.     
  389.     def __getitem__(self, model):
  390.         model = model.lower()
  391.         
  392.         try:
  393.             return self._ModelData__cache[model]
  394.         except:
  395.             log.debug('Cache miss: %s' % model)
  396.             log.debug('Reading file: %s' % self.released_dat)
  397.             if self.read_section(self.released_dat, model):
  398.                 return self._ModelData__cache[model]
  399.             return { }
  400.  
  401.  
  402.     
  403.     def all_models(self):
  404.         return self._ModelData__cache
  405.  
  406.     
  407.     def get_data_type(self, key):
  408.         
  409.         try:
  410.             return self.FIELD_TYPES[key]
  411.         except KeyError:
  412.             
  413.             try:
  414.                 return self.FIELD_TYPES_DYN[key]
  415.             except KeyError:
  416.                 
  417.                 try:
  418.                     return self.TYPE_CACHE[key]
  419.                 except KeyError:
  420.                     for pat, typ in self.RE_FIELD_TYPES.items():
  421.                         match = pat.match(key)
  422.                         if match is not None:
  423.                             self.TYPE_CACHE[key] = typ
  424.                             return typ
  425.                     
  426.                 except:
  427.                     match is not None
  428.                 
  429.  
  430.                 None<EXCEPTION MATCH>KeyError
  431.             
  432.  
  433.             None<EXCEPTION MATCH>KeyError
  434.  
  435.         log.error('get_data_type(): Field type lookup failed for key %s' % key)
  436.  
  437.     
  438.     def convert_data(self, key, value, typ = None):
  439.         if typ is None:
  440.             typ = self.get_data_type(key)
  441.         
  442.         if typ in (TYPE_BITFIELD, TYPE_INT):
  443.             
  444.             try:
  445.                 value = int(value)
  446.             except (ValueError, TypeError):
  447.                 log.error('Invalid value in .dat file: %s=%s' % (key, value))
  448.                 value = 0
  449.             except:
  450.                 None<EXCEPTION MATCH>(ValueError, TypeError)
  451.             
  452.  
  453.         None<EXCEPTION MATCH>(ValueError, TypeError)
  454.         if typ == TYPE_BOOL:
  455.             value = utils.to_bool(value)
  456.         elif typ == TYPE_LIST:
  457.             value = _[1]
  458.         elif typ == TYPE_DATE:
  459.             if datetime_avail:
  460.                 match = self.date.search(value)
  461.                 if match is not None:
  462.                     month = int(match.group(1))
  463.                     day = int(match.group(2))
  464.                     year = int(match.group(3))
  465.                     value = datetime.date(year, month, day)
  466.                 
  467.             
  468.         elif typ == TYPE_HEX:
  469.             
  470.             try:
  471.                 value = int(value, 16)
  472.             except (ValueError, TypeError):
  473.                 log.error('Invalid hex value in .dat file: %s=%s' % (key, value))
  474.                 value = 0
  475.             except:
  476.                 None<EXCEPTION MATCH>(ValueError, TypeError)
  477.             
  478.  
  479.         None<EXCEPTION MATCH>(ValueError, TypeError)
  480.         return value
  481.  
  482.  
  483.